package com.hjx.windowtool.eh;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class EhHost {
	public static void main(String[] args) throws Exception {
		Map<String, List<String>> map = initMap();
        
        
        Set<Entry<String,List<String>>> entrySet = map.entrySet();
        List<String> listHosts=new ArrayList<>();
        for (Entry<String, List<String>> e : entrySet) {
			List<String> value = e.getValue();
			for (String string : value) {
				listHosts.add(string+" "+e.getKey());
			}
		}
        listHosts.sort(new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				return o1.substring(o1.indexOf(' ')+1).compareTo(o2.substring(o2.indexOf(' ')+1));
			}
		});
        List<String> oldHost = Files.readAllLines(Paths.get("C:\\Windows\\System32\\drivers\\etc\\hosts"));
        Files.write(Paths.get("C:\\Windows\\System32\\drivers\\etc\\hosts.bak"), oldHost);
        for (String string : oldHost) {
			if (oldHost.contains(string)) {
				continue;
			}
		}
        oldHost.addAll(listHosts);
        Files.write(Paths.get("C:\\Windows\\System32\\drivers\\etc\\hosts"), oldHost);
        
        Runtime.getRuntime().exec("ipconfig /flushdns");
	}

	/**
	 * @return
	 */
	public static Map<String, List<String>> initMap() {
		Map<String, List<String>> map = new HashMap<>();
        put(map, "e-hentai.org", "104.20.134.21", "104.20.135.21", "172.67.0.127");
        put(map, "exhentai.org", "178.175.128.252", "178.175.129.252", "178.175.129.254", "178.175.128.254",
                "178.175.132.20", "178.175.132.22");
        put(map, "repo.e-hentai.org", "94.100.28.57", "94.100.29.73");
        put(map, "forums.e-hentai.org", "94.100.18.243");
        put(map, "ehgt.org", "37.48.89.44", "81.171.10.48", "178.162.139.24", "178.162.140.212"
                , "2001:1af8:4700:a062:8::47de", "2001:1af8:4700:a062:9::47de", "2001:1af8:4700:a0c9:4::47de", "2001:1af8:4700:a0c9:3::47de");
        put(map, "gt0.ehgt.org", "37.48.89.44", "81.171.10.48", "178.162.139.24", "178.162.140.212"
                , "2001:1af8:4700:a062:8::47de", "2001:1af8:4700:a062:9::47de", "2001:1af8:4700:a0c9:4::47de", "2001:1af8:4700:a0c9:3::47de");
        put(map, "gt1.ehgt.org", "37.48.89.44", "81.171.10.48", "178.162.139.24", "178.162.140.212"
                , "2001:1af8:4700:a062:8::47de", "2001:1af8:4700:a062:9::47de", "2001:1af8:4700:a0c9:4::47de", "2001:1af8:4700:a0c9:3::47de");
        put(map, "gt2.ehgt.org", "37.48.89.44", "81.171.10.48", "178.162.139.24", "178.162.140.212"
                , "2001:1af8:4700:a062:8::47de", "2001:1af8:4700:a062:9::47de", "2001:1af8:4700:a0c9:4::47de", "2001:1af8:4700:a0c9:3::47de");
        put(map, "gt3.ehgt.org", "37.48.89.44", "81.171.10.48", "178.162.139.24", "178.162.140.212"
                , "2001:1af8:4700:a062:8::47de", "2001:1af8:4700:a062:9::47de", "2001:1af8:4700:a0c9:4::47de", "2001:1af8:4700:a0c9:3::47de");
        put(map, "ul.ehgt.org", "94.100.24.82", "94.100.24.72");
        put(map, "raw.githubusercontent.com", "151.101.0.133", "151.101.64.133", "151.101.128.133", "151.101.192.133");
		return map;
	}

	private static void put(Map<String, List<String>> map,String key, String... string) {
		List<String> list = map.getOrDefault(key, new ArrayList<>());
		for (String s : string) {
			list.add(s);
		}
		map.put(key, list);
	}
}
